package com.quanquan.parking.ketop.service.statisticsReport;

import com.quanquan.parking.ketop.repository.statisticsReport.KetopParkingReportRepository;
import com.quanquan.parking.request.PayNotifyRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import utils.DateUtils;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 功能描述:优惠券统计报表
 *
 * @ClassName: CouponReportService
 * @Author: xiongjunbao
 * @Date: 2019-05-30 11:31
 * @Version: V1.0
 */
@Service
public class KetopParkingReportService {

    public static final String DALITY = "daily";
    public static final String WEEK = "week";
    public static final String MONTH = "month";
    public static final String YEAR = "year";

    private final KetopParkingReportRepository parkingReportRepository;

    @Autowired
    public KetopParkingReportService(KetopParkingReportRepository parkingReportRepository) {
        this.parkingReportRepository = parkingReportRepository;
    }

    /**
     * 停车总量统计 分日、周、月、年
     *
     * @return
     */
    public Map<String, Object> parkingNum(@RequestBody PayNotifyRequest request) {
        // 获取当前时间
        Date date = new Date();
        // 统计周期 日 dality  周 week 月 month 年 year
        String statisticalCycle = request.getStatisticalCycle();
        List<Object[]> byTypeByDay;
        Map<String, Object> map = new LinkedHashMap<>();
        if (statisticalCycle.equals(DALITY)) {
            byTypeByDay = parkingReportRepository.findCountByDay();
            map = formatData(byTypeByDay);
            return formatDay(map);
        } else if (statisticalCycle.equals(WEEK)) {
            byTypeByDay = parkingReportRepository.findCountByWeek();
            map = formatData(byTypeByDay);
            return formatWeek(map);
        } else if (statisticalCycle.equals(MONTH)) {
            byTypeByDay = parkingReportRepository.findCountByMonth();
            map = formatData(byTypeByDay);
            return formatMonth(map);
        } else if (statisticalCycle.equals(YEAR)) {
            byTypeByDay = parkingReportRepository.findCountByYear();
            map = formatData(byTypeByDay);
            return formatYear(map);
        } else {
            System.out.println("参数错误");
        }
        return map;
    }

    /**
     * 停车费统计 分日、周、月、年
     *
     * @return
     */
    public Map<String, Object> parkingFeeNum(@RequestBody PayNotifyRequest request) {
        // 获取当前时间
        Date date = new Date();
        // 统计周期 日 dality  周 week 月 month 年 year
        String statisticalCycle = request.getStatisticalCycle();
        List<BigDecimal[]> byTypeByDay;
        Map<String, Object> map = new LinkedHashMap<>();
        if (statisticalCycle.equals(DALITY)) {
            byTypeByDay = parkingReportRepository.findFeeByDay();
            map = formatDataBig(byTypeByDay);
            return formatDay(map);
        } else if (statisticalCycle.equals(WEEK)) {
            byTypeByDay = parkingReportRepository.findFeeByWeek();
            map = formatDataBig(byTypeByDay);
            return formatWeek(map);
        } else if (statisticalCycle.equals(MONTH)) {
            byTypeByDay = parkingReportRepository.findFeeByMonth();
            map = formatDataBig(byTypeByDay);
            return formatMonth(map);
        } else if (statisticalCycle.equals(YEAR)) {
            byTypeByDay = parkingReportRepository.findFeeByYear();
            map = formatDataBig(byTypeByDay);
            return formatYear(map);
        } else {
            System.out.println("参数错误");
        }
        return map;
    }



    /**
     * 按日整理数据
     *
     * @param map
     * @return
     */
    public Map<String, Object> formatDay(Map<String, Object> map) {
        Map<String, Object> newMap = new LinkedHashMap<>();
        List<String> weekDateList = DateUtils.dateList(7);
        System.out.println(weekDateList);
        for (String weekDate : weekDateList) {
            //如果传入的map中包含这个键，不动
            if (map.containsKey(weekDate.substring(0, 10))) {
                newMap.put(weekDate.substring(5, 10), map.get(weekDate.substring(0, 10)));
            } else {
                //如果不包含补0
                newMap.put(weekDate.substring(5, 10), 0);
            }
        }
        return newMap;
    }

    /**
     * 按周整理数据
     *
     * @param map
     * @return
     */
    public Map<String, Object> formatWeek(Map<String, Object> map) {
        Map<String, Object> newMap = new LinkedHashMap<>();
        List<String> weekList = DateUtils.weekList(7);
        System.out.println(weekList);
        for (String week : weekList) {
            //如果传入的map中包含这个键，不动
            if (map.containsKey(week.substring(0, 7))) {
                newMap.put(week.substring(0, 7), map.get(week.substring(0, 7)));
            } else {
                //如果不包含补0
                newMap.put(week.substring(0, 7), 0);
            }
        }
        return newMap;
    }

    /**
     * 按月整理数据
     *
     * @param map
     * @return
     */
    public Map<String, Object> formatMonth(Map<String, Object> map) {
        Map<String, Object> newMap = new LinkedHashMap<>();
        List<String> monthList = DateUtils.monthList(7);
        System.out.println(monthList);
        for (String month : monthList) {
            //如果传入的map中包含这个键，不动
            if (map.containsKey(month.substring(0, 7))) {
                newMap.put(month.substring(0, 7), map.get(month.substring(0, 7)));
            } else {
                //如果不包含补0
                newMap.put(month.substring(0, 7), 0);
            }
        }
        return newMap;
    }

    /**
     * 按年整理数据
     *
     * @param map
     * @return
     */
    public Map<String, Object> formatYear(Map<String, Object> map) {
        Date date = new Date();
        String year = DateUtils.dateToString(date, DateUtils.DATE).substring(0, 4);
        Map<String, Object> newMap = new LinkedHashMap<>();
        //如果传入的map中包含这个键，不动
        if (map.containsKey(year.substring(0, 4))) {
            newMap.put(year.substring(0, 4), map.get(year.substring(0, 4)));
        } else {
            //如果不包含补0
            newMap.put(year.substring(0, 4), 0);
        }
        return newMap;
    }


    /**
     * 重新封装数据到map中
     *
     * @param byTypeByDay
     * @return
     */
    public Map<String, Object> formatData(List<Object[]> byTypeByDay) {
        Map<String, Object> map = new LinkedHashMap<>();
        for (Object[] objects : byTypeByDay) {
            map.put((String) objects[0], (BigInteger) objects[1]);
        }
        return map;
    }

    /**
     * 重新封装数据到map中
     *
     * @param byTypeByDay
     * @return
     */
    public Map<String, Object> formatDataBig(List<BigDecimal[]> byTypeByDay) {
        Map<String, Object> map = new LinkedHashMap<>();
        for (Object[] objects : byTypeByDay) {
            DecimalFormat df = new DecimalFormat("0.00");
            double totalFee = Double.valueOf(objects[1].toString());
            String totalFeeYuan = df.format(totalFee / 100);
            map.put((String) objects[0], totalFeeYuan);
        }
        return map;
    }


}
